This notebook is absolutely based on PyActigraphy, a package developed by Grégory Hammad (https://github.com/ghammad/pyActigraphy)
import pyActigraphy
import plotly.graph_objs as go
import pandas as pd
import numpy as np
rawAWD = pyActigraphy.io.read_raw_awd('example_10.CSV')
rawAWD.start_time
rawAWD.frequency
rawAWD.data
rawAWD.data.index
rawAWD.data.sum()
rawAWD.data.mean()
rawAWD.data.hist()
rawAWD.data.idxmax()
rawAWD.data.idxmin()
data = rawAWD.data
GT1000 = rawAWD.data[data > 1000] # values greater than 1000 or any specified value
GT1000index = rawAWD.data.index[data > 1000]
GT1000.sum()
from plotly.graph_objs import Figure, FigureWidget, Layout, Scatter
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
layout = Layout(
title="Raw Actimetry data",
xaxis=dict(title="Date time"),
yaxis=dict(title="Activity/phase"),
showlegend=False
)
iplot(Figure(data=[Scatter(x=rawAWD.data.index, y=rawAWD.data)], layout=layout))
layout1 = Layout(
title="Raw Actimetry values greater than 1000",
xaxis=dict(title="Date time"),
yaxis=dict(title="Activity/phase"),
showlegend=False
)
iplot(Figure(data=[Scatter(x=GT1000index, y=GT1000, mode='markers',)], layout=layout1))
CK = rawAWD.CK()
np.savetxt("CK.csv", CK, delimiter=",")
layout = go.Layout(title="Rest/Activity detection",xaxis=dict(title="Date time"), yaxis=dict(title="Counts/period"), showlegend=False)
layout.update(yaxis2=dict(title='Classification',overlaying='y',side='right'), showlegend=True);
go.Figure(data=[
go.Scatter(x=rawAWD.data.index.astype(str),y=rawAWD.data, name='Data'),
go.Scatter(x=CK.index.astype(str),y=CK, yaxis='y2', name='CK')
], layout=layout)
crespo = rawAWD.Crespo()
crespo_6h = rawAWD.Crespo(alpha='6h')
crespo_zeta = rawAWD.Crespo(estimate_zeta=True)
go.Figure(data=[
go.Scatter(x=rawAWD.data.index.astype(str),y=rawAWD.data, name='Data'),
#go.Scatter(x=crespo.index.astype(str),y=crespo, yaxis='y2', name='Crespo'),
#go.Scatter(x=crespo_6h.index.astype(str),y=crespo_6h, yaxis='y2', name='Crespo (6h)'),
#go.Scatter(x=CK.index.astype(str),y=CK, yaxis='y2', name='CK'),
go.Scatter(x=crespo_zeta.index.astype(str),y=crespo_zeta, yaxis='y2', name='Sleep')
], layout=layout)
A_Onset = rawAWD.AonT(freq='10min', binarize=True)
A_Onset
A_Offset = rawAWD.AoffT(freq='10min', binarize=True)
A_Offset
rawAWD.IS()
rawAWD.IS(freq='30min', binarize=True, threshold=4)
rawAWD.IS(freq='1H', binarize=False)
rawAWD.IVm()
rawAWD.IV(freq='1H', binarize=False)
rawAWD.IVm()
rawAWD.IV()
rawAWD.IV(freq='1H', binarize=False)
rawAWD.RA()
rawAWD.RA(binarize=False)
rawAWD.M10()
rawAWD.L5()
Used when light exposition data is loaded, instead of rest-activity data. M16 and M3
def _lmx(data, period, lowest=True):
"""Calculate the start time and mean activity of the period of
lowest/highest activity"""
avgdaily = _average_daily_activity(data, cyclic=True)
n_epochs = int(pd.Timedelta(period)/avgdaily.index.freq)
mean_activity = avgdaily.rolling(period).sum().shift(-n_epochs+1)
if lowest:
t_start = mean_activity.idxmin()
else:
t_start = mean_activity.idxmax()
lmx = mean_activity[t_start]/n_epochs
return t_start, lmx
def _average_daily_activity(data, cyclic=False):
"""Calculate the average daily activity distribution"""
avgdaily = data.groupby([
data.index.hour,
data.index.minute,
data.index.second
]).mean()
if cyclic:
avgdaily = pd.concat([avgdaily, avgdaily])
index = pd.timedelta_range(
start='0 day',
end='2 days',
freq=data.index.freq,
closed='left'
)
else:
index = pd.timedelta_range(
start='0 day',
end='2 day',
freq=data.index.freq,
closed='left'
)
avgdaily.index = index
return avgdaily
def M16(self, binarize=True, threshold=4):
if binarize is True:
data = self.binarized_data(threshold)
else:
data = self.data
_, M16 = _lmx(data, '16H', lowest=False)
return M16
def M3(self, binarize=True, threshold=4):
if binarize is True:
data = self.binarized_data(threshold)
else:
data = self.data
_, M16 = _lmx(data, '3H', lowest=False)
return M16
M16(rawAWD, binarize=False, threshold=4)
M3(rawAWD, binarize=False, threshold=4)
data = rawAWD.data
L_onset = _lmx(data, 30, lowest=True)
L_onset
M_onset= _lmx(data, 30, lowest=False)
M_onset
Average = rawAWD.average_daily_activity(freq='60min', cyclic=False, binarize=True, threshold=4)
Average
Average_Activity = np.sum(Average, axis=0)
Average_Activity
daily_profile = rawAWD.average_daily_activity(freq='60min', cyclic=True, binarize=True, threshold=4)
daily_profile.mean()
daily_profile.median()
from scipy import ndimage
ndimage.measurements.center_of_mass(daily_profile) # to calculate the center of gravity or centroid (/2 if 60_)
ndimage.measurements.center_of_mass(daily_profile.index) # to calculate the center of gravity or centroid (/2 if 60_)
daily_profile.sum() # 2x Lux, because of cyclic=True on daily profile calculation
daily_profile.idxmax()
daily_profile.idxmin()
ProfileGTx = daily_profile[daily_profile>40] # have to specify the value (standard: > 1000 counts)
ProfileGTx.sum()
ProfileGTx.mean()
ProfileGTx.idxmax()
ProfileGTx.idxmin()
ProfileGTxindex = ProfileGTx.index[ProfileGTx>40]
ProfileGTxindex
layout.update(title="24-h profile - Double plot",xaxis=dict(title="Time"), showlegend=False);
go.Figure(data=[
go.Scatter(x=daily_profile.index.astype(str), y=daily_profile)
], layout=layout)
layout1 = Layout(
title="24-h Profile with values greater than X ",
xaxis=dict(title="Date time"),
yaxis=dict(title="Activity/phase"),
showlegend=False
)
iplot(Figure(data=[Scatter(x=ProfileGTxindex, y=ProfileGTx, mode='markers',)], layout=layout1))
rawAWD.ADAT(binarize=True, threshold=4)
#rawAWD.ADATp(period='7D', binarize=True, threshold=4, verbose=False)
rawAWD.ADATp(period= '7D', binarize=True, threshold=4, verbose=True)
daily_profile1 = rawAWD.average_daily_activity(freq='60min', cyclic=True, binarize=True, threshold=4)
AUC_total = (np.trapz(daily_profile1, axis=0)/2)
AUC_total
Soff = 5 # example
Son = 21 # example
Soff2 = 29 # example, to compute sleep duration
df = pd.DataFrame(np.array(daily_profile), columns=['y'])
AUC_wake = np.trapz(df['y'][Soff:Son])
AUC_Sleep = np.trapz(df['y'][Son:Soff2])
AUC_wake
AUC_Sleep
from plotly.subplots import make_subplots
from plotly.graph_objs import Figure, FigureWidget, Layout, Scatter
from plotly.offline import init_notebook_mode, iplot
layout.update(title="Partial Activity profile - Wake",xaxis=dict(title="Time"), showlegend=False);
init_notebook_mode(connected=True)
go.Figure(data=[
go.Scatter(x=daily_profile.index.astype(str), y = (df['y'][Soff:Son]))
], layout=layout)
layout.update(title="Partial Activity profile - Sleep",xaxis=dict(title="Time"), showlegend=False);
go.Figure(data=[
go.Scatter(x=daily_profile.index.astype(str), y = (df['y'][Son:Soff2]))
], layout=layout)
daily_profile_luz = rawAWD.average_daily_activity(freq='60min', cyclic=True, binarize=True, threshold=4)
Lon = 6 # example of natural LD, sunrise = 6h, sunset = 18h
Loff = 18
Lon2 = 30
df2 = pd.DataFrame(np.array(daily_profile_luz), columns=['y'])
AUC_Day = np.trapz(df2['y'][Lon:Loff])
AUC_Night = np.trapz(df2['y'][Loff:Lon2])
AUC_Day
AUC_Night
from plotly.subplots import make_subplots
from plotly.graph_objs import Figure, FigureWidget, Layout, Scatter
from plotly.offline import init_notebook_mode, iplot
layout.update(title="Partial Activity profile - Day",xaxis=dict(title="Time"), showlegend=False);
init_notebook_mode(connected=True)
go.Figure(data=[
go.Scatter(x=daily_profile.index.astype(str), y = (df2['y'][Lon:Loff]))
], layout=layout)
layout.update(title="Partial Activity profile - Night",xaxis=dict(title="Time"), showlegend=False);
go.Figure(data=[
go.Scatter(x=daily_profile.index.astype(str), y = (df2['y'][Loff:Lon2]))
], layout=layout)
AUC_Sunset_SleepOnset = np.trapz(df2['y'][Loff:Son])
AUC_Sunset_SleepOnset
layout.update(title="Partial Activity profile - Sunset to Sleep Onset",xaxis=dict(title="Time"), showlegend=False);
go.Figure(data=[
go.Scatter(x=daily_profile.index.astype(str), y = (df2['y'][Loff:Son]))
], layout=layout)
Important to run light exposition data instead of rest-activity
log10_value = np.log10(rawAWD.data)
log10_value
layout = Layout(
title="Raw Activity data",
xaxis=dict(title="Date time"),
yaxis=dict(title="Activity/phase"),
showlegend=False
)
iplot(Figure(data=[Scatter(x=rawAWD.data.index, y=log10_value)], layout=layout))